<template>
    <main-wrap :loading="true" ref="mianEle" :load="onLoad" :end="page.finished" :noFixed="true" bodyBg="none" :showOverTxt="page.lists.length>0" :pageIndex="page.pageIndex">
        <template v-slot:header>
            <div class="adheader">
                <div class="hcon">
                    <div class="shopLetfTop">
                        <b>{{total}}</b> {{totalText}}
                    </div>
                    <div class="topSearch">
                        <div class="tswd">
                            <my-search :placeholder="search" v-model="keyword"  @onSearch="getSearchData"/>
                        </div>
                    </div>
                </div>
            </div>
        </template>
        <div class="goodsMainCon">
            <div class="ctgbox">
                <two-tab v-model="goodsCategoryId" :lists="goodsCategorys" @change="getSearchData" keyValue="goodsCategoryId" numValue='goodsNum'/>
            </div>
            <!-- <div></div> -->
            <div class="goodsTabSearchBox">
                <sort :lists="tabList" @change="getSortData"/>
            </div>
            <goods-list :lists="page.lists" :type="1"/>
            <empty v-if="page.finished && page.lists.length<1"/>
        </div>
    </main-wrap>
</template>

<script>
import {reactive,toRefs, onMounted,computed,getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
import mainWrap from '@/components/mainWrap.vue'
import {toast} from '@/utils/common.js'
import oneTab from '@/components/oneTab.vue';
import mySearch from '@/components/mySearch.vue'
import Image from '@/components/image.vue';
import myTab from '@/components/myTab.vue';
import goodsList from '@/components/goodsList.vue';
import sort from '@/components/sort.vue';
import twoTab from '@/components/twoTab.vue'
import empty from '@/components/empty.vue'
import { useI18n } from 'vue-i18n'

export default {
    components:{
        mainWrap,
        oneTab,
        mySearch,
        Image,
        myTab,
        goodsList,
        sort,
        twoTab,
        empty
    },
    setup(){
        const store = useStore()
        const {proxy} = getCurrentInstance()
        const {t} = useI18n()

        const state = reactive({
            keyword:'',
            goodsCategoryId:'',
            goodsCategorys:[],
            sortParams:{
                orderField: 1,
                orderType: "desc",
            },
            page:{
                lists:[],
                pageIndex:1,
                pageSize:20,
                finished: false
            },
            total:0,
            totalText:t('public.total'),
            search:t('public.search'),
            tabList:[{id:1,name:t('shop.text4'),noShow:true},{id:2,name:t('shop.text5')}]
        })

        onMounted(()=>{
           onLoad()
           getGoodsCtg()
        })

        const onLoad = ()=> {
            // 此函数必须返回一个Promise对象
            return new Promise((resolve, reject) => {
                setTimeout(async () => {
                    const res = await store.dispatch("shop/get_goods_list", {
                        page: state.page.pageIndex,
                        limit: state.page.pageSize,
                        ...state.sortParams,
                        goodsCategoryId:state.goodsCategoryId,
                        keyWord:state.keyword,
                        //shopId:state.id
                    });
                    state.page.lists = [...state.page.lists, ...res.data];
                    state.page.pageIndex++;
                    state.total = res.total
                    if (state.page.lists.length >= res.total) {
                        state.page.finished = true;
                    }
                    resolve(true);
                }, 500);
            });
        }

        const getGoodsCtg = ()=>{
            store.dispatch('shop/get_goods_type_list',{
                goodsName: '',
                shopId: ''
            })
            .then(res =>{
                const goodsTotal = res.data.reduce(function(total,item){return total+item.goodsNum},0)
                state.goodsCategorys = [{goodsCategoryId:'',name:'全部',goodsNum:goodsTotal},...res.data]
            })
        }
        
        const resetScroll = async()=>{
            proxy.$refs.mianEle.stopScroll()
            state.page.lists=[]
            state.page.pageIndex=1
            state.page.finished=false
            await onLoad()
            proxy.$refs.mianEle.initScroll()
        }
        const getSearchData = async(keyword)=>{
            resetScroll()
        } 
        const getSortData = sortParams =>{
            state.sortParams = sortParams
            resetScroll()
        }

        return {
            ...toRefs(state),
            onLoad,
            getSearchData,
            getSortData
        }
    }
}
</script>

<style lang="scss" scoped>
    .adheader{
         padding: 15px 30px 15px 0;
        .hcon{
            display: flex;
            height: 80px;
            line-height: 80px;
            align-items: center;
            .shopLetfTop{
                padding: 0;
                font-size: 30px;
                font-weight: 500;
                b{
                    color: #82FEF4;
                }
            }
            .topSearch{
                flex: 1;
                // display: flex;
                // justify-content: end;
                .tswd{
                    float:right;
                    width: 550px;
                }
            }
        }
    }
    .goodsMainCon{
        padding: 10px 0 0 0;
    }
    .goodsTabSearchBox{
        display: inline-block;
        height: 55px;
        background: url(/images/gloabAdSplace/tsc.png) left top no-repeat;
        background-size: 100% 100%;
        padding: 0 30px;
    }
    .ctgbox{
        // display: inline-block;
        // width: auto;
        padding: 0 0 20px 0;
    }

</style>